package bsh;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import bsh.compilation.DebugOptions;
import bsh.exception.CompilationAssertionFailed;
import bsh.exception.CompilationFailedException;
import bsh.meta.Scope;
import bsh.meta.TypeWrapper;

/**
 * NodeCompiler que vai inserir os comentarios do script como LDC e depois um POP.
 * 
 * @author takeshi
 */
public class CommentAsLDCCompiler implements NodeCompiler {

    private final BSHFormalComment comment;

    private final CompilationUnit unit;

    /**
     * C'tor
     * 
     * @param comment comment
     * @param unit unit
     */
    public CommentAsLDCCompiler(BSHFormalComment comment, CompilationUnit unit) {
        super();
        this.comment = comment;
        this.unit = unit;
    }

    /**
     * {@inheritDoc}
     * 
     * @see bsh.NodeCompiler#compile(org.objectweb.asm.MethodVisitor, bsh.CompilationState)
     */
    public void compile(MethodVisitor host, CompilationState state) throws CompilationFailedException {
        if (this.unit.options().debugOptions().contains(DebugOptions.COMMENTS_AS_LDC)) {
            host.visitLdcInsn(this.comment.text);
            host.visitInsn(Opcodes.POP);
            state.addLineNumber(host, this.comment.getLineNumber());
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see bsh.NodeCompiler#nodeResultType(bsh.meta.Scope)
     */
    public TypeWrapper nodeResultType(Scope scope) throws CompilationFailedException {
        throw new CompilationAssertionFailed("should not have to ask for a comment's type");
    }

}
